<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods, i) in goodList" :key="i"  @click="gotoDetag(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			//请求参数对象
			queryObj: {
				query: '',
				cid: '',
				pagenum: 1,
				pagesize: 10
			},
			goodList: [],
			total: 0,
			isloading:false
		};
	},
	onLoad(options) {
		console.log(options);
		this.queryObj.query = options.query || '';
		this.queryObj.cid = options.cid || '';
		this.getGoodsList();
	},
	methods: {
		// 点击商品跳转商品详情页
		gotoDetag(goods){
			// console.log('2222');
			uni.navigateTo({
				url:'/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
			});
		},
		//获取商品列表数据的方法 cb是一个函数
		async getGoodsList(cb) {
			//打开节流
			this.isloading=true
			// 3.1 发起请求
			const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
			//关闭节流
			this.isloading=false
			//如果cb存在 便调用cb
			cb && cb()
			// 3.2 请求失败
			// console.log(res);
			if (res.meta.status !== 200) return uni.$showMsg();
			// 3.3 请求成功，为 data 中的数据赋值
			this.goodList = [...this.goodList,...res.message.goods]
			this.total = res.message.total;
		}
	},
	//底部上啦刷新
	onReachBottom() {
		if(this.queryObj.pagenum *this.queryObj.pagesize >=this.total) return  uni.$showMsg('数据加载完毕')
		if(this.isloading) return
		//让页码值加一
		this.queryObj.pagenum++
		this.getGoodsList()
	},
	//顶部上啦刷新
	onPullDownRefresh() {
		//重置关键数据
		this.queryObj.pagenum=1
		this.total=0
		this.isloading=false
		this.goodList=[]
		//重新发起数据请求
		this.getGoodsList(()=>uni.stopPullDownRefresh())
	}
};
</script>

<style lang="scss">

</style>
